<?php
class Page_admin extends Think{//分页类
//var $table;//表名
var $n;//每页显示条数
var $d;//当前页
var $num;//总条数
var $j;//一共几页
var $start;//起始位置
var $url;//当前文件
var $py;//偏移几个单位,以当前页为中心对称偏移
var $yc;//溢出多少个单位，以3,5,7.....

function Getallnum($sql){//得到总条数



  //global $db;
 // $this->table=$table;

  $Model	=	new Model();
  $all=$Model->query($sql);
  $this->num=count($all);
  return $this->num;
}
function Getallpage($n){//得到一共几页
  $this->n=$n;
  $this->j=ceil($this->num/$this->n);
  return $this->j;
}
function Getpage($d,$url){//分页
  $d=$_GET['page'];
  $this->d=$d;
  $this->url=$url;
  if (empty($this->d) || $this->d <0 || $this->d==0 || $this->d==1){
   $this->d=1;
   if(($this->j)>1){
    $str="首页";
	$str.="上一页";
    $str.=$this->mathurl(1,3);
    $str.="<a href='".$this->url."page/".($this->d+1)."'>下一页</a>";
	$str.="<a href='".$this->url."page/".$this->j."'>尾页</a>";
   }
  }elseif(($this->d)>=$this->j){
   $this->d=$this->j;
   $str="<a href='".$this->url."page/1'>首页</a>";
   $str.="<a href='".$this->url."page/".($this->d-1)."'>上一页</a>";
   $str.=$this->mathurl(1,3);
   $str.="下一页";
   $str.="尾页";
  }else{
   $str="<a href='".$this->url."page/1'>首页</a>";
   $str.="<a href='".$this->url."page/".($this->d-1)."'>上一页</a>";
   $str.=$this->mathurl(1,3);
   $str.="<a href='".$this->url."page/".($this->d+1)."'>下一页</a>";
   $str.="<a href='".$this->url."page/".$this->j."'>尾页</a>";
  }
  return $str;
  return $this->d;
  return $this->url;
}
function Getstart(){//起始位置
  if(empty($this->d)){
   $this->start=0;
  }else{
   $this->start=$this->n*($this->d-1);
  }
  return $this->start;
}
function Getend($e){//每页显示条数
  return $this->d=$e;
}
function options(){//跳转菜单
  $opt=" <input type='text' size='2' style='height: 16px; border:1px solid #ccc;' onkeydown=\"javascript: if(event.keyCode==13) location='".$this->url."page='+this.value;\">";
  return $opt;
}
function mathurl($py,$yc){//数字导航
  $this->py=$py;
  $this->yc=$yc;
  $c1="co";
  $co=$_GET['page'];//当前页码
  $do=$this->j;//总页数

  if($this->j<=$this->yc){//总页数小于或等于$yc时

   for ($o=1;$o<=$this->j;$o++){

       if($o==$co){
        $murl.=" ".$co." ";
        }else{
            $murl.=" <a  href='".$this->url."page/".$o."'>".$o."</a> ";
        }
    //$murl.=" <a  href='".$this->url."page=".$o."'>".$o."</a> ";
   }
  }elseif($co>($do-$this->py)){//页码超过总页数时
   $co=$do;
   for ($v=($co-($this->yc-1));$v<=$do;$v++){//往左偏移$yc个单位
    if($co==$v){
        $murl.=" ".$v." ";
    }else{
        $murl.=" <a  href='".$this->url."page/".$v."'>".$v."</a> ";
    }
   }
  }
  elseif($co<=$this->py)
 {
   $co=1;
   for ($v=$co;$v<($co+$this->yc);$v++)
  {//往右偏移$yc个单位

    //$murl.=" <a  href='".$this->url."page=".$v."'>".$v."</a> ";
   if($co==$v){
        $murl.=" ".$v." ";
    }else{
        $murl.=" <a  href='".$this->url."page/".$v."'>".$v."</a> ";
    }
   }
  }
  else{

   for($k=($co-$this->py);$k<$co;$k++){//往左偏移$py个单位
   $murl.=" <a  href='".$this->url."page/".$k."'>".$k."</a> ";
   }
   for ($v=$co;$v<=($co+$this->py);$v++){//往右偏移$py个单位
    //$murl.=" <a  href='".$this->url."page=".$v."'>".$v."</a> ";
    if($co==$v){
        $murl.=" ".$v." ";
    }else{
        $murl.=" <a  href='".$this->url."page/".$v."'>".$v."</a> ";
    }
   }
  }
  return $murl;
}
}